Credit Card Expiration Date DropDownList Sample Code

The other day I needed to create a credit card input form including the drop down lists for the credit card expiration month and year.  I started to write the code to populate the month and year drop down lists and I wanted to make them dynamic so that whatever the year was it would add an appropriate number of list items.  But as I started thinking about how to structure the code, I figured that this has been written probably thousands of times by other developers so I would just Google for it.  To my surprise, I was unable to pull back any sample code for my various search terms.  In the end I had to write it myself but now I am posting it on my blog so that others my benefit from my hard work.  :)

//Populate the credit card expiration month drop down
for (int i = 1; i <= 12; i++)
{
    DateTime month = new DateTime(2000, i, 1);
    ListItem li = new ListItem(month.ToString("MMM (M)"), month.ToString("MM"));
    ExpirationDateMonthDropDown.Items.Add(li);
}
ExpirationDateMonthDropDown.Items[0].Selected = true;

//Populate the credit card expiration year drop down (go out 12 years) 
for (int i = 0; i <= 11; i++)
{
    String year = (DateTime.Today.Year + i).ToString();
    ListItem li = new ListItem(year, year);
    ExpirationDateYearDropDown.Items.Add(li);
}
ExpirationDateYearDropDown.Items[0].Selected = true;

 

Technorati Tags:

7 Comments

  • Setting the selected property is unnecessary since the first item in the dropdown is selected by default.

  • Thanks for sharing this code with all of us. I have a recommondation for the hardcoded numbers in your code. For improving the readability use constants for 12 (of months), 2000 (in the loop of the month and 11 (years).

  • This code is not really well written actually. You create a lot of DateTime variables which you don't need at all.

    CultureInfo ui = CultureInfo.CurrentUICulture;

    string[] monthNames = ui.DateTimeFormat.MonthNames;
    int monthsInYear = ui.Calendar.GetMonthsInYear(DateTime.Now.Year);

    int monthNumber = 0;
    var monthsDataSource = monthNames.Take(monthsInYear).Select(monthName => new {
    Name = monthName,
    Value = ++monthNumber
    });

    monthsDropDownList.DataTextField = "Name";
    monthsDropDownList.DataValueField = "Value";
    monthsDropDownList.DataSource = monthsDataSource;

    monthsDropDownList.DataBind();

    Would be better.. imho.

    Cheers,
    Wes

  • Thanks for the code, Jeff! In the past the company that I work for had hard-coded years into credit card expiration date drop down lists which is a big pain to update.

    You could also set the selected month to the current month like:

    ExpirationDateMonthDropDown.SelectedValue = DateTime.Now.ToString("MM");

    That way the default selected month/year value isn't in the past.

  • Thank you for all your hard work - the code worked great and saved me alot of time. Your the best..

  • Thanks for the sample! Appreciate it very much...

  • I find this works well for my CascadingDropDown and you're not creating any DateTime variables. If you want the month as an integer for the value just remove change the value to "i.Tostring()" instead.

    [WebMethod]
    public CascadingDropDownNameValue[]
    GetMonths(string knownCategoryValues, string category)
    {
    List values = new List();

    //Get the next 12 months
    for (int i = 1; i <= 12; i++)
    {
    values.Add(new CascadingDropDownNameValue(i.ToString("D2"), i.ToString("D2")));
    }

    return values.ToArray();
    }

Comments have been disabled for this content.